<%@page isELIgnored="false" pageEncoding="UTF-8" contentType="text/html; UTF-8" %>
    <script type="text/javascript">
        $(function () {
            $('#ssSupplier').searchbox({
                searcher:function(value,name){
                    $("#tableSupplier").datagrid("load",{
                        "columnName":name,
                        "columnValue":value,
                    });
                },
                width:300,
                menu:'#mmSupplier',
                prompt:'请输入值',
            });

            $("#tableSupplier").datagrid({
                url: "${pageContext.request.contextPath}/back/supplier/findAll",
                autoRowHeight: true,
                fit:true,
                fitColumns: true,
                toolbar: '#tbSupplier',
                columns: [[
                    {field: "checkbox", checkbox: true},
                    {title: "名称", field: "name", width: 100},
                    {title: "logo", field: "logo", width: 100,formatter: function (value, row, index) {
                            return "<img style='width:80px;height:80px;border-radius:50%;display:block;' src='${pageContext.request.contextPath}/"+row.logo+"'/>";
                        }},
                    {
                        title: "操作", field: "options", width: 100, formatter: function (value, row, index) {
                            return "<a onclick=showSupplier(\""+row.id+"\") class='btnSupplier' data-options=\"iconCls:'icon-edit',\">更新</a>";
                        }
                    }
                ]],
                pagination: true,
                checkOnSelect: true,
                selectOnCheck: true,
                pageList:[5,10,20,40,80,100],
                pageSize:5,
                onLoadSuccess: function () {
                    $(".btnSupplier").linkbutton();
                }
            });
        });

        //打开添加对话框
        function openAddSupplierDialog() {
            $("#addSupplierDialog").dialog({
                title:"添加供货商信息",
                iconCls:'icon-save',
                width:600,
                height:400,
                modal:true,
                href:"${pageContext.request.contextPath}/back/supplier/add.jsp",
                buttons:[{
                    text:'保存',
                    iconCls:'icon-save',
                    handler:saveSupplier,
                },{
                    text:'关闭',
                    iconCls:'icon-cancel',
                    handler:function(){
                        $("#addSupplierDialog").dialog("close");
                    }
                }]
            });
        }
        //保存供货商信息
        function saveSupplier(){
            $('#saveSupplierForm').form({
                url:'${pageContext.request.contextPath}/back/supplier/add',
                onSubmit: function(){
                        return $("#saveSupplierForm").form("validate");
                },
                success:function(result){
                    result = eval("("+ result +")");
                    $.messager.show({
                        title:'提示信息',
                        msg:result.message,
                        timeout:5000,
                        showType:'slide'
                    });
                    $('#addSupplierDialog').dialog("close");
                    $("#tableSupplier").datagrid("reload");
                }
            });
            $('#saveSupplierForm').submit();
        }
        //更新供货商信息
        function updateSupplier(){
            $('#updateSupplierForm').form({
                url:'${pageContext.request.contextPath}/back/supplier/update',
                onSubmit: function(){
                        return $("#updateSupplierForm").form("validate");
                },
                success:function(result){
                    result = eval("("+ result +")");
                    $.messager.show({
                        title:'提示信息',
                        msg:result.message,
                        timeout:5000,
                        showType:'slide'
                    });
                    $('#updateSupplierDialog').dialog("close");
                    $("#tableSupplier").datagrid("reload");
                }
            });
            $('#updateSupplierForm').submit();
        }
        //展示供货商信息
        function showSupplier(id){

            $("#updateSupplierDialog").dialog({
                title:"更新供货商信息",
                iconCls:'icon-edit',
                width:600,
                height:400,
                modal:true,
                href:"${pageContext.request.contextPath}/back/supplier/update.jsp",
                buttons:[{
                    text:'保存',
                    iconCls:'icon-save',
                    handler:updateSupplier,
                },{
                    text:'关闭',
                    iconCls:'icon-cancel',
                    handler:function(){
                        $("#updateSupplierDialog").dialog("close");
                    }
                }],
                onLoad:function(){
                    $("#updateSupplierForm").form('load','${pageContext.request.contextPath}/back/supplier/findOne?id='+id);
                    var t = setInterval(function () {
                        if($("#logoPath").val()){
                            $("#logo").attr("src","${pageContext.request.contextPath}/"+$("#logoPath").val());
                            window.clearInterval(t);
                        }
                    },1);
                }
            });
        }
    </script>
<table id="tableSupplier" class="easyui-datagird"></table>

<div id="tbSupplier">
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true,onClick:openAddSupplierDialog,">添加</a>

    <input id="ssSupplier"></input>
    <div id="mmSupplier" style="width:120px">
        <div data-options="name:'name',iconCls:'icon-ok'">名称</div>
    </div>
</div>

<%--添加对话框--%>
<div id="addSupplierDialog"></div>
<%--更新对话框--%>
<div id="updateSupplierDialog"></div>
